const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
var search = async ctx =>{
    let {kw} = ctx.query;
    console.log(kw);//示例id：123692817
    var value = encodeURIComponent(kw);
    console.log(value);
    /* 1.新建一个浏览器的实例 */
    const browser = await puppeteer.launch({
        headless:false,
        defaultViewport:null
    });
    /* 2.新建一个页面 */
    const page = await browser.newPage();
    /* 3.到达某个页面 */
    await page.goto(`https://www.duitang.com/search/?kw=${value}&type=feed`);
    await page.waitForTimeout(3000)
    //获取数据
    let content = await page.content();
    let $ = cheerio.load(content,{
        decodeEntities:false
    })

    let arr=[];
    $(".woo").each((index,value)=>{
        let imageUrl = $(value).find(".mbpho img").attr("src");
        let title = $(value).find(".wooscr .g").html();
        let like = $(value).find(".d2 span").html();
        let star = $(value).find(".d1 span").html();
        let userImg =  $(value).find(".f img").attr("src");
        let userName = $(value).find(".f .p").html();
        let userLabel = $(value).find(".f p span").html().split("<")[0]+$(value).find(".f p span a").html();
        let user = []
        user.push({
            userImg,
            userName,
            userLabel
        })
        arr.push({
            imageUrl,
            title,
            like,
            star,
            user
        })
    })
    ctx.body = arr;
}
module.exports = search;